﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Diagnostics;
using System.Windows.Controls.Primitives;

namespace WP7_calendar
{
    public partial class MainPage : PhoneApplicationPage
    {
        public List<Model.CalendarItem> Calendar;
        ViewModel.MainPageModel Model; // Primary Model instance

        public MainPage()
        {
            InitializeComponent();
            Model = new ViewModel.MainPageModel();
            WeekList.ItemsSource = Model.Month.Calendar;
            lblSelectedDate.Text = Model.Day.SelectedDate;
        }

        /// <summary>
        /// Handles when a user presses a date button on the big calendar
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e">Contains information about which button was pressed</param>
        private void MonthBtnClickEvent(object sender, RoutedEventArgs e)
        {
            string caller = (e.OriginalSource as Button).Name;
            Model.Day.SelectedDate = caller;
            lblSelectedDate.Text = caller;

            tbDay.Text = Model.Day.SelectedDate.Substring(Model.Day.SelectedDate.IndexOf('/')+1,((Model.Day.SelectedDate.Substring(Model.Day.SelectedDate.IndexOf('/')+1,2).Contains('/')) ? 1 : 2));
            tbMonth.Text = Model.Day.SelectedDate.Substring(0, Model.Day.SelectedDate.IndexOf('/'));
            tbYear.Text = Model.Day.SelectedDate.Substring(Model.Day.SelectedDate.IndexOf('/', 3)+1);
            tbHour.Text = "8";
            tbMinute.Text = "00";

            Debug.WriteLine("Called by: " + caller);

            AgendaList.ItemsSource = Model.Day.AgendaItems;
        }

		///<summary>
        /// Show Add apointment popup menu
        /// </summary>
        private void btnAddAppointment_Click(object sender, RoutedEventArgs e)
        {
            popAddAppointment.IsOpen = true;
        }

        /// <summary>
        /// Remove tool tip for Appointment header
        /// </summary>
        private void tbTitle_GotFocus(object sender, RoutedEventArgs e)
        {
            if (tbTitle.Text == "Title")
            {
                tbTitle.Text = "";
            }
        }

        /// <summary>
        /// Remove tool tip for Appointment Location
        /// </summary>
        private void tbLocation_GotFocus(object sender, RoutedEventArgs e)
        {
            if (tbLocation.Text == "Location")
            {
                tbLocation.Text = "";
            }
        }

        /// <summary>
        /// Submits the create appointment popup form
        /// </summary>
        private void btnSubmitAppointment_Click(object sender, RoutedEventArgs e)
        {
            popAddAppointment.IsOpen = false;
            string date = String.Format("{0},{1},{2}", tbMonth.Text, tbDay.Text, tbYear.Text);
            string time = tbHour.Text + ":" + tbMinute.Text;

            List<Model.AgendaItem> list = new List<WP7_calendar.Model.AgendaItem>();
            
            Model.Day.AgendaItems.Add(new Model.AgendaItem(tbTitle.Text, date, time, tbLocation.Text));
            Model.Day.SaveItem();
            AgendaList.ItemsSource = Model.Day.AgendaItems;
        }

        /// <summary>
        /// Handles when the user presses the Facebook button
        /// </summary>
        private void btnLoadFacebook_Click(object sender, RoutedEventArgs e)
        {
            Model.Day.UpdateFacebook();
        }
    }
}